﻿using Fast.ConfigureOptions;
using Microsoft.Extensions.Configuration;

namespace Fast.Extensions.RabbitMQ
{
    /// <summary>
    /// RabbitMQ配置选项
    /// </summary>
    [SkipScan, ConfigureOptions(JsonKey = "RabbitMQ")]
    public class FastRabbitMqOptions : IConfigureOptions<FastRabbitMqOptions>
    {
        /// <summary>
        /// RabbitMQ连接列表
        /// </summary>
        public RabbitMqConnections Connections { get; } = new RabbitMqConnections();

        /// <summary>
        /// 连接失败重试次数
        /// </summary>
        public int ConnectionFailRetryCount { get; set; }

        public void PostConfigure(FastRabbitMqOptions options, IConfiguration configuration)
        {
            if (options.ConnectionFailRetryCount == 0)
            {
                options.ConnectionFailRetryCount = 5;
            }

            foreach (var connectionFactory in options.Connections.Values)
            {
                connectionFactory.DispatchConsumersAsync = true;
            }
        }
    }
}